package com.company.test26;

import java.io.*;
import java.util.Scanner;

/**
 * Created by Administrator on 2016.11.8.
 */
public class Test2622 {
    public static void main(String[] args) throws IOException{
        Scanner input=new Scanner(System.in);
        String fileName=input.nextLine();

        File file=new File(fileName);

        try {
            input=new Scanner(file);
        }catch (FileNotFoundException e) {
            System.out.println("该文件不存在");
            return;
        }

        String text="";

        while (input.hasNext()){
            text=text+input.nextLine()+"\n";
        }

        System.out.println("ok");

        int[] huffmanCode=HuffmanCode.getCharacterFrequency(text);

        File newFile=new File(fileName+".new");
        File huf=new File(fileName+".huf");

        DataOutputStream output=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(newFile)));
        ObjectOutputStream hufOutput=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(huf)));

        hufOutput.writeObject(huf);

        for(int i=0;i<text.length();i++){
            output.writeInt(huffmanCode[text.charAt(i)]);
        }
        output.close();
        hufOutput.close();
    }
}